<?php
/**
 * CopyHelper.php class file.
 * @author wudg <544038230@qq.com>
 * @date 2021/1/26- 上午11:04
 */

namespace common\helpers;
use phpDocumentor\Reflection\File;
use Yii;

class CopyHelper
{


    /**
     * 返回字体绝对路径地址
     * @param string $font_type
     * @return bool|string
     */
    public static function getFontTypeMapping(string $font_type)
    {
        $fontFilePath = Yii::getAlias('@common/static/font/'.$font_type);

        //字体不存在默认一个字体
        if(!file_exists($fontFilePath))
        {
            $fontFilePath = Yii::getAlias('@common/static/font/Baoli.ttc');
        }

        return $fontFilePath;
    }

    /**
     * 处理水印设置问题
     * @param array $waterSetting
     * @param string $waterPath
     * @return array
     */
    public static function formatWaterSetting(array $waterSetting, string $waterPath = null)
    {
        $mappingSetting = [];
        if(isset($waterPath) && !empty($waterPath) && isset($waterSetting['avatar_type'])
            && $waterSetting['avatar_type'] == 1
        )
        {
            $mappingSetting['water_path'] = $waterPath;

        }elseif(isset($waterSetting['avatar_type']) && $waterSetting['avatar_type'] == 2) //文字水印
        {
            $mappingSetting['txt_option'] =[];
            if(isset($waterSetting['avatar_text_content']) && !empty($waterSetting['avatar_text_content']))
            {
                $mappingSetting['water_txt'] = trim($waterSetting['avatar_text_content']);
            }
            //字体大小
            if(isset($waterSetting['avatar_text_font_size']) && !empty($waterSetting['avatar_text_font_size']))
            {
                $mappingSetting['txt_option']['size'] = trim($waterSetting['avatar_text_font_size']);
            }
            //颜色
            if(isset($waterSetting['avatar_text_color']) && !empty($waterSetting['avatar_text_color']))
            {
                $mappingSetting['txt_option']['color'] = str_replace('#','',trim($waterSetting['avatar_text_color']));
            }
            //角度
            if(isset($waterSetting['avatar_text_rotate']))
            {
                $waterSetting['avatar_text_rotate'] = $waterSetting['avatar_text_rotate'] > 80 ? 80 : $waterSetting['avatar_text_rotate'];
                $waterSetting['avatar_text_rotate'] = $waterSetting['avatar_text_rotate'] < -80 ? -80 : $waterSetting['avatar_text_rotate'];
                $mappingSetting['txt_option']['angle'] = (int)$waterSetting['avatar_text_rotate'];
            }
            if(isset($waterSetting['avatar_text_font_type']) && !empty($waterSetting['avatar_text_font_type']))
            {
                $mappingSetting['font_type'] = self::getFontTypeMapping(trim($waterSetting['avatar_text_font_type']));
            }
        }

        $mappingSetting['water_position'] = isset($waterSetting['water_position']) ? intval($waterSetting['water_position']) : 1;

        return $mappingSetting;

    }



    /**
     * 获取属性值单位
     * @param string $valueUnit
     * @param string $value
     * @return mixed
     */
    public static function getUnit(string $valueUnit,string $value)
    {
        if($valueUnit)
        {
            $unitArr = explode(',',$valueUnit);

            foreach($unitArr as $k => $unit)
            {
                if(strpos($value,$unit) !== false)
                {
                    return $unit;
                }
            }
            return $unitArr[0];
        }

        return '';

    }
}